package com.xiaoran.robot.service.impl;

import com.xiaoran.robot.entity.WsCos;
import com.xiaoran.robot.mapper.WsCosMapper;
import com.xiaoran.robot.service.WsCosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class WsCosServiceImpl implements WsCosService {
    @Autowired
    WsCosMapper mapper;
    @Override
    public void insertList(List<WsCos> datalist) {
        for (WsCos wsCos : datalist) {
            Integer postId = wsCos.getPostId();
            if(idList().contains(postId)){
                // 防止重复数据
                continue;
            }
            mapper.insert(wsCos);
        }
    }

    @Override
    public List<Integer> idList() {
        List<WsCos> wsCos = mapper.selectList(null);
        List<Integer> idList = new ArrayList<>();
        for (WsCos wsCo : wsCos) {
            idList.add(wsCo.getPostId());
        }
        return idList;
    }

    @Override
    public String oneCos() {
        List<WsCos> wsCos = mapper.selectList(null);
        StringBuilder sb = new StringBuilder();
        for (WsCos wsCo : wsCos) {
            sb.append(wsCo.getImages());
        }
        String[] picList = sb.toString().split("\\|");
        Random random = new Random();
        int randNum = random.nextInt(picList.length);
        return picList[randNum];
    }
}
